﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using FreeProperty.Views.Shared;
using FreeProperty.Data;

namespace FreeProperty.Views.ProviderDirectory
{
    public partial class SearchPassportProviderResults : SiteBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session[FreePropertyConstants.SESSION_SEARCH_OBJECT])
            {
                Response.Redirect("~/Views/ProviderDirectory/SearchPassportProvider.aspx");
            }
            if (!IsPostBack)
            {
                if (null != Session[FreePropertyConstants.SESSION_PROVIDER_SEARCH])
                {
                    List<ProviderVO> lst = (List<ProviderVO>)Session[FreePropertyConstants.SESSION_PROVIDER_SEARCH];
                    lblResult.Text = lst.Count.ToString() + " providers were found matching your search criteria.";
                    hdnCurrentPage.Value = "1";
                    hdnPageCount.Value = PagingUtil.GetPagesCount(lst.Count).ToString();
                    BindPagingData();
                    Session[FreePropertyConstants.SESSION_PROVIDER_SEARCH] = null;
                }
            }
            if (hdnStatus.Value.Length > 0)
            {
                switch (hdnStatus.Value.ToUpper())
                {

                    case "PAGING":
                        break;
                    case "PREVIOUS":
                        hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) - 1).ToString();
                        break;
                    case "NEXT":
                        hdnCurrentPage.Value = (Convert.ToInt32(hdnCurrentPage.Value) + 1).ToString();
                        break;
                    case "FIRST":
                        hdnCurrentPage.Value = "1";
                        break;
                    case "LAST":
                        hdnCurrentPage.Value = hdnPageCount.Value;
                        break;


                }
                BindPagingData();
            }
        }
        protected void BindPagingData()
        {
            int currentPage = Convert.ToInt32(hdnCurrentPage.Value);
            int startingIndex = (currentPage - 1) * PagingUtil.PageSize;
            int itemsToFetch = PagingUtil.PageSize;
            List<ProviderVO> lstProviders = FreePropertyUtil.GetSearchResults(); 
            if ((startingIndex + itemsToFetch) > lstProviders.Count)
            {
                itemsToFetch = lstProviders.Count - startingIndex;
            }
            List<ProviderVO> lst = lstProviders.GetRange(startingIndex, itemsToFetch);
            PagingUtil.BindDataToGrid(grdSearchResults, lst, "Views/ProviderDirectory/SearchPassportProviderResults.aspx", "AD", spanFootServerPaging, hdnCurrentPage, hdnPageCount, lnkFootNext, lnkFootLast, lnkFootFirst, lnkFootPrevious);
        }
        

        protected void grdSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ProviderVO oProvider = (ProviderVO)e.Row.DataItem;
                GridViewRow row = e.Row;
                Image imgA = (Image)row.FindControl("imgServicesA");
                Image imgY = (Image)row.FindControl("imgServicesY");
                Image imgC = (Image)row.FindControl("imgServicesC");

                imgA.Visible = false;
                imgY.Visible = false;
                imgC.Visible = false;

                //Provider objProvider = new Provider();
                //var list = objProvider.SearchProviders();
                 if (oProvider.Services == "A")
                        imgA.Visible = true;
                    else if (oProvider.Services == "Y")
                        imgY.Visible = true;
                    else if (oProvider.Services == "C")
                        imgC.Visible = true;

                HyperLink  lnkMap = (HyperLink)row.FindControl("lnkMap");
                StringBuilder sbAddress = new StringBuilder();
                sbAddress.Append(oProvider.Address).Append(" ").Append(oProvider.City).Append(" ").Append(oProvider.State).Append(" ").Append(oProvider.ZipCode);

                var url = "http://maps.google.co.in/maps?f=q&source=s_q&hl=en&geocode=&q=" + HttpContext.Current.Server.UrlEncode(sbAddress.ToString());
                lnkMap.NavigateUrl = url;

                // Distance showing logic 
                if (null != Session[FreePropertyConstants.SESSION_SEARCH_OBJECT])
                {
                    SearchParametersVO oSearchParametersVO = (SearchParametersVO)Session[FreePropertyConstants.SESSION_SEARCH_OBJECT];
                    if (oSearchParametersVO.IsDistanceSearch)
                    {
                        ZipCodeVO zipProvider = oSearchParametersVO.ListDistanceZipCodes.Find(t => t.ZipCode.Equals(oProvider.ZipCode));
                        if (zipProvider != null)
                        {
                            Label lblDistanceInfo = (Label)row.FindControl("lblDistanceInfo");
                            lblDistanceInfo.Text = zipProvider.DistanceInMiles + " mile(s) from Zipcode " + oSearchParametersVO.Zipcode + " .";
                        }
                    }
                }
                


            }

        }

       
        
    }
}